<template>
  <div>
    <el-button
      class="back-btn"
      type="primary"
      icon="el-icon-arrow-left"
      plain
      style="margin-right: 25px"
      @click="goBack"
      >返回</el-button
    >
    <div class="warp" v-loading="loadState">
      <iframe class="frame" :src="encodedUrl" ref="frame"></iframe>
    </div>
  </div>
</template>

<script>
import { FILE_PREVIEW_URL } from "~/Contants";

export default {
  name: "CommonFilePreviewer",
  props: {
    src: String,
  },
  data() {
    return {
      loadState: true,
    };
  },
  methods: {
    goBack() {
      this.$emit("exit");
    },
    onload() {
      this.loadState = false;
    },
  },
  computed: {
    encodedUrl() {
      console.log(this.src);
      return `${FILE_PREVIEW_URL}?url=${encodeURIComponent(btoa(this.src))}`;
    },
  },
  mounted() {
    const that = this;
    const frame = this.$refs.frame;
    this.loadState = true;
    frame.onload = (...args) => that.onload(...args);
  },
};
</script>

<style scoped>
.back-btn {
  position: absolute;
  top: 45px;
  right: 35px;
  z-index: 100;
}
.warp {
  position: absolute;
  overflow: hidden;
  top: 5px;
  left: 5px;
  right: 10px;
  bottom: 5px;
  z-index: 5;
  text-align: center;
  background-color: white;
}
.frame {
  width: 100%;
  height: 100%;
}
</style>
